import { defineStore } from 'pinia';
import { cardList } from './cardStore.type';
type Card = {
    cartid: string
    discount: number
    flag: boolean
    img1: string
    num: number
    originprice: number
    proid: string
    proname: string
}
export const useCardListStore = defineStore('cardListStore', {
    state: (): cardList => {
        return {
            cardList: [{
                cartid: '',
                discount: 0,
                flag: false,
                img1: '',
                num: 0,
                originprice: 0,
                proid: '',
                proname: '',
            }],
            checked: false,
            price: 0,
            keyword: '',
            oldKeyword: [],
            cities:''

        }
    },
    actions: {
        setCardList(cardList: Card[]) {
            (this.cardList as unknown as Card[]) = cardList;
            this.setFlag();
        },
        setFlag() {
            if (this.cardList) {
                // this.checked = true;
                // this.cardList.forEach(item => {
                //     if (!item.flag) {
                //         this.checked = false
                //     }
                // })
                this.checked = this.cardList.every(item => item.flag == true)
            }
        },

    },
    getters: {
        totalPrice(state) {
            state.price = 0;
            if (this.cardList) {
                this.cardList.forEach(item => {
                    if (item.flag) {
                        state.price += item.originprice * item.num
                    }
                })
            }
            return state.price * 100;
        }
    }
})


